//
//  SYSYDownloadImage.m
//  HA
//
//  Created by sy on 2023/12/17.
//

#import "SYDownloadImage.h"
#import "SDWebImageDownloader.h"
#import "SDImageCache.h"

@implementation SYDownloadImage

+(void)po_sysdownloadImagesWith:(NSArray *)array complete: (void(^)(UIImage *imaege, NSInteger index))complete {
    
    SDImageCache *cache = [SDImageCache sharedImageCache];
    SDWebImageDownloader *loader = [SDWebImageDownloader sharedDownloader];
    
    for (NSInteger i = 0; i < array.count; i ++) {
        id img = array[i];
        if ([img isKindOfClass:[NSString class]]) {
            UIImage *sd_image = [cache imageFromCacheForKey:img];
            if (sd_image) {
                if (complete) {
                    complete(sd_image,i);
                }
            }else{
                [loader downloadImageWithURL:[NSURL URLWithString:img] options:SDWebImageDownloaderHighPriority progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
                } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
                    if (image) {
                        if (complete) {
                            complete(image,i);
                        }
                        [cache storeImage:image forKey:img completion:nil];
                    }
                }];
            }
        }else if ([img isKindOfClass:[UIImage class]]) {
            if (complete) {
                complete(img,i);
            }
        }
    }
}

+(void)po_synDownloadImagesWith:(NSArray *)array complete: (void(^)(NSArray *imaeges))complete {
    
    SDWebImageDownloaderConfig *config = [[SDWebImageDownloaderConfig alloc]init];
    SDWebImageDownloader *downloader = [[SDWebImageDownloader alloc]initWithConfig:config];
    
    dispatch_group_t dispatchGroup = dispatch_group_create();
    
    __block NSMutableArray *imageArray = [NSMutableArray array];

    for (int i = 0; i < array.count; i ++) {
        id img = array[i];
        if ([img isKindOfClass:[NSString class]]) {
            dispatch_group_enter(dispatchGroup);
            [downloader downloadImageWithURL:[NSURL URLWithString:img] completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
                if (image) {
                    [imageArray addObject:image];
                }
                dispatch_group_leave(dispatchGroup);
            }];
        }else if ([img isKindOfClass:[UIImage class]]) {
            [imageArray addObject:img];
        }
    }
    dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^(){
        if (complete) {
            complete(imageArray);
        }
    });
}


//计算商品图片尺寸
+(CGSize)po_ImageHeightWith:(UIImage *)image width:(CGFloat)width {
    CGSize imageSize = image.size;
    CGSize size;
    size.width = width;
    size.height = size.width * imageSize.height / imageSize.width;
    return size;
}

+(void)sy_sysdownloadImageWith:(NSString *)url complete: (void(^)(UIImage *image, NSError * _Nullable error))complete {
    
    SDImageCache *cache = [SDImageCache sharedImageCache];
    SDWebImageDownloader *loader = [SDWebImageDownloader sharedDownloader];
    
    UIImage *sd_image = [cache imageFromCacheForKey:url];
    if (sd_image) {
        if (complete) {
            complete(sd_image,nil);
        }
    }else {
        [loader downloadImageWithURL:[NSURL URLWithString:url] options:SDWebImageDownloaderHighPriority progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
        } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
            if (complete) {
                complete(image,error);
            }
            [cache storeImage:image forKey:url completion:nil];
        }];
    }
}

//取消图片下载
+(void)sy_cancelLoadImageWith:(NSString *)url {
//    SDWebImageDownloader *loader = [SDWebImageDownloader sharedDownloader];
//    SDWebImageDownloadToken *token = [[SDWebImageDownloadToken alloc]init];
//    token.url = [NSURL URLWithString:url];
//    [loader cancel:token];
}


@end
